package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.fragment.formatDate
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition7
import com.abc.robot01.retrofit.bean.Condition70
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch


class SystemMessageFragmentViewModel : ViewModel() {
    val TAG = "SystemMessageFragmentViewModel"
    val customerName = mutableStateOf("")
    val totalAmount = mutableStateOf("15.08")

    val count = mutableStateOf("0")
    val customerPhone = mutableStateOf("")
    val show = mutableStateOf(false)
    val isLeft = mutableStateOf(false)


    val selectedDateMillis = mutableStateOf(0L)
    val startDate =  mutableStateOf(0L)
    val endDate =  mutableStateOf(0L)

    fun  reset(){
        selectedDateMillis.value =0L
        startDate.value =0L
        endDate.value =0L
    }

    //客户列表
    private val _customerList = MutableStateFlow<List<RetrofitService.RenderItem>>(emptyList())
    val customerList: StateFlow<List<RetrofitService.RenderItem>> = _customerList

    fun load() {
        viewModelScope.launch {
            val res = RetrofitService.instance.getListRenderTask(
                accessToken = Robot.accessToken,
                companyId = Robot.companyId,
                request = FetchDataRequest9(
                    condition = Condition70(
                    ).apply {
                        if(startDate.value!=0L){
                             this.endTimeStart = formatDate(startDate.value)+" 00:00:00"
                        }
                        if(endDate.value!=0L){
                            this.endTimeEnd = formatDate(endDate.value)+" 23:59:59"
                        }
                    }
                )
            )
            Log.e(TAG, "系统渲染记录:${res.data.list!!.size}")
            Log.e(TAG, "系统渲染记录:$res")

            res.data.list.forEach {
                Log.e(TAG, "系统渲染记录:$it")
            }
            if (res.code == 200) {
                _customerList.value = res.data.list
                count.value = res.data.list.size.toString()
            }
        }
    }
}
